package ylzy.com.yanglezhiyang;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class Floor {

    private static final String TAG = "Floor";
    public ArrayList<GameObject> spirits = new ArrayList<>();
    public int count = 0;
    public boolean isMoveDownHalf = true;//
    public boolean isStack = false;//
    public StackDirection stackDirection = StackDirection.LEFT;//0
    public int x;
    public int y;

    public enum StackDirection {LEFT, RIGHT, UP}

    public Floor(){

    }

    public Floor(int count){
        this.count = count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setXY(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void setMoveDownHalf(boolean moveDownHalf) {
        isMoveDownHalf = moveDownHalf;
    }

    public void setStack(boolean stack) {
        this.isStack = stack;
    }

    public void setStackDirection(StackDirection stackDirection) {
        this.stackDirection = stackDirection;
    }

    public void addSpirit(GameObject gameObject){
        this.spirits.add(gameObject);
    }

    public void addSpirits(ArrayList<GameObject> spirits){
        this.spirits.addAll(spirits);
    }

    public boolean checkCanAdd(){
        if(spirits.size()<=count){
            return true;
        }
        return false;
    }

    public void layout(){
        if(!isStack){
            Point[] ps = LevelUtil.getLayout(isMoveDownHalf);
            Random r = new Random();
            HashMap<Integer, Integer> map = new HashMap<>();
            for(GameObject go : spirits){
                while (true){
                    int index = Math.abs(r.nextInt()%ps.length);
                    if(!map.containsKey(index)){
                        Point p = ps[index];
                        map.put(index, index);
                        go.layout(p.x, p.y, LevelUtil.getSpiritWidth(), LevelUtil.getSpiritHeight(), 0, 0);
                        break;
                    }
                }
            }
        }else{
            int padding = 10;
            if(stackDirection == StackDirection.UP){
                for(int i=spirits.size(); i>0; i--){
                    GameObject go = spirits.get(i-1);
                    go.layout(x, y-padding*(spirits.size()-i+1), LevelUtil.getSpiritWidth(), LevelUtil.getSpiritHeight(), 0, 0);
                }
            }else if(stackDirection == StackDirection.LEFT){
                for(int i=spirits.size(); i>0; i--){
                    GameObject go = spirits.get(i-1);
                    go.layout(x-padding*(spirits.size()-i+1), y, LevelUtil.getSpiritWidth(), LevelUtil.getSpiritHeight(), 0, 0);
                }
            }else if(stackDirection == StackDirection.RIGHT){
                for(int i=spirits.size(); i>0; i--){
                    GameObject go = spirits.get(i-1);
                    go.layout(x+padding*(spirits.size()-i+1), y, LevelUtil.getSpiritWidth(), LevelUtil.getSpiritHeight(), 0, 0);
                }
            }
        }
    }

    public void remove(GameObject temp){
        temp.floor = null;
        spirits.remove(temp);
    }

    public void draw(Canvas canvas, Paint paint){
        for(GameObject gameObject : spirits){
            gameObject.draw(canvas,paint);
        }
    }
}
